<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <!-- logo区域 -->
      <div class="login-head">
        <div class="logo">logo</div>
      </div>
      <!-- 表单区域 -->
      <el-form class="login-form" ref="form" :model="form" :rules="rules">
        <!-- 手机号码区域 -->
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <!-- 密码区域 -->
        <el-form-item prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <!-- 同意协议区域 -->
        <el-form-item prop="isAgree">
          <el-checkbox v-model="form.isAgree" label="我已阅读并同意....." name="type"></el-checkbox>
        </el-form-item>
        <!-- 点击登录区域 -->
        <el-form-item>
          <el-button
          :loading="isLoading"
          class="login-btn" type="primary" @click="hLogin">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: 'login',
  data () {
    return {
      isLoading: false,
      form: {
        mobile: '13911111111',
        code: '246810',
        isAgree: true
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入合法的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '验证码必须是6位纯数字', trigger: 'blur' }
        ],
        isAgree: [
          {
            validator: function (rule, value, callback) {
              if (value) {
                callback()
              } else {
                callback(new Error('请阅读协议并选择同意!'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },

  methods: {
    doLogin () {
      const { mobile, code } = this.form
      this.isLoading = true
      this.$axios({
        url: '/mp/v1_0/authorizations',
        method: 'post',
        data: {
          mobile,
          code
        }
      }).then(res => {
        // 保存token
        localStorage.setItem('token', res.data.data.token)
        this.$message({
          type: 'success',
          message: '登录成功'
        })
        this.isLoading = false
        this.$router.push('/')
      }).catch(err => {
        this.$message.error('登录失败了')
        this.isLoading = false
        console.log(err)
      })
    },
    hLogin () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.doLogin()
        }
      })
    }
  }

}
</script>
<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
